package great.easychat.help.dialog;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.provider.Settings;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import androidx.core.app.ActivityCompat;
import androidx.core.content.PermissionChecker;
import great.easychat.help.R;

public class PhonePemissionDialog {
    private ImageView ivPhoneState;
    private ImageView ivStrongState;
    private Button btnOpen;
    private AlertDialog dialog = null;
    public interface Callback {
        void dismiss();

    }


    private Callback mcallback;
    /**
     * @param context  上下文
     * @param callback 回调
     */
    Activity activity;
    public void show(Activity context, final Callback callback) {
        if (dialog == null) {
            activity = context;
            dialog = new AlertDialog.Builder(context).create();
            mcallback = callback;
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
            dialog.setCancelable(false);//屏蔽返回按钮
            if (!context.isFinishing()) {
                dialog.show();
            }
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            window.setAttributes(lp);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
            window.setContentView(R.layout.dialog_phone_permission);
            window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            ivPhoneState = window.findViewById(R.id.ivPhoneState);
            ivStrongState = window.findViewById(R.id.ivStrongState);
            btnOpen = window.findViewById(R.id.btnOpen);

            btnOpen.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (!ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) || !ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_PHONE_STATE)) {
                        // 如果选择了 禁止后不再询问 需要跳到系统设置权限界面
                        gotoPermission();
                    } else {
                        ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE}, 0);
                    }
                }
            });
            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    if (callback != null) {
                        callback.dismiss();
                    }
                }
            });

        }
    }

    /**
     * 取消窗口
     */
    public void cancelDialog() {
        try {
            if (dialog != null) {
                dialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showDialog() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    private void gotoPermission() {
        //引导用户至设置页手动授权
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", activity.getApplicationContext().getPackageName(), null);
        intent.setData(uri);
        activity.startActivity(intent);
    }


    public void refresh() {
        if (PermissionChecker.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            ivStrongState.setImageResource(R.drawable.permission_btn_radio_choice_s);
        } else {
            ivStrongState.setImageResource(R.drawable.permission_btn_radio_choice_n);
        }

        if (PermissionChecker.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            ivPhoneState.setImageResource(R.drawable.permission_btn_radio_choice_s);
        } else {
            ivPhoneState.setImageResource(R.drawable.permission_btn_radio_choice_n);
        }

        if (PermissionChecker.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED && PermissionChecker.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
           cancelDialog();
        }
    }


}
